package com.team.instantsharing.ultils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Password {
	private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
	private static final String SALT = "cloudsharing";

	private static String bytesToHex(byte[] bytes) {
		char[] hexChars = new char[bytes.length * 2];
		int v;
		for (int j = 0; j < bytes.length; j++) {
			v = bytes[j] & 0xFF;
			hexChars[j * 2] = HEX_ARRAY[v >>> 4];
			hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
		}
		return new String(hexChars);
	}

	public static String createPassword(String plainText) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			md.update(SALT.getBytes());
			md.update(plainText.getBytes());

			byte[] password = md.digest();
			return bytesToHex(password);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}
//	public static void main(String[] args) {
//		System.out.println(createPassword("123456"));
//	}
	
}
